/**
 * Project: Error on line 2, column 15 in Templates/JSF/JSFManagedBean.java
 * Package: com.made2ordersoftware.daycare.control
 * File: TimesWatched
 * Created: Oct 30, 2011
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.control;

import com.made2ordersoftware.control.AbstractCtrl;
import com.made2ordersoftware.daycare.business.ChildMgr;
import com.made2ordersoftware.daycare.control.util.JsfUtil;
import com.made2ordersoftware.daycare.domain.enums.WatchCode;
import com.made2ordersoftware.daycare.service.TimeWatchedDao;
import com.made2ordersoftware.exceptions.BusinessException;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.util.ExceptionHandler;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Control class for all times watched actions.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Named("timeWatched")
@RequestScoped
public class TimesWatchedCtrl extends AbstractCtrl {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(TimesWatchedCtrl.class);
    private Long parentId;
    private Long childId;
    private Date dateIn_Out;
    private WatchCode watchCode;
    @Inject private ChildMgr manager;
    /* Fields needed for control purposes only. */
    @Inject private TimeWatchedDao dao;
    private int hour;
    private int minute;
    private String meridiem;
    private final String[] hours = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
    private final String[] minutes = {"00", "15", "30", "45"};
    private final String[] meridiems = {"AM", "PM"};
    private WatchCode watchCodes;

    /**
     * Creates a new instance of TimesWatched
     */
    public TimesWatchedCtrl() {
    }

    /**
     * Used to sign a child into the day care systems.
     *
     * @return next page to go to
     */
    public String signInChild() {
        LOG.info("Starting signing in of child.");
        String nextPage = null;
        Exception exp = null;
        String message = null;
        try {
            manager.signInChild(getParentId(), getChildId(), getTimestamp(), getWatchCode());
            message = ResourceBundle.getBundle("/Bundle").getString("child_signed_in");
            nextPage = "/home";
            LOG.info("Child was signed in successfully.");
        } catch (DAOException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("server_error_occurred");
            nextPage = "/serverError";
        } catch (BusinessException e) {
            exp = e;
            message = e.getMessage();
        } finally {
            if (exp != null) {
                ExceptionHandler.handleException(exp);
                JsfUtil.addErrorMessage(message);
            } else {
                JsfUtil.addSuccessMessage(message);
            }
        }
        return nextPage;
    }

    /**
     * Used to sign a child out of the day care systems.
     *
     * @return next page to go to
     */
    public String signOutChild() {
        String nextPage = null;
        Exception exp = null;
        String message = null;
        try {
            manager.signOutChild(getChildId(), getTimestamp());
            message = "child_signed_out";
            nextPage = "/home";
        } catch (DAOException e) {
            message = "server_error_occurred";
            nextPage = "serverError";
            return nextPage;
        } catch (BusinessException e) {
            exp = e;
            message = e.getCause().toString();
            return nextPage;
        } finally {
            if (exp != null) {
                ExceptionHandler.handleException(exp);
                JsfUtil.addErrorMessage(ResourceBundle.getBundle("/Bundle").getString(message));
            } else {
                JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString(message));
            }
        }
        return nextPage;
    }

    /**
     * ID of parent signing in child.
     *
     * @return the parentId
     */
    public Long getParentId() {
        return parentId;
    }

    /**
     * ID of parent.
     *
     * @param parentId the parentId to set
     */
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    /**
     * ID of child being signed into day care.
     *
     * @return the childId
     */
    public Long getChildId() {
        return childId;
    }

    /**
     * ID of current child.
     *
     * @param childId the childId to set
     */
    public void setChildId(Long childId) {
        this.childId = childId;
    }

    /**
     * Date child is signed in or out of day care.
     *
     * @return the dateIn_Out
     */
    public Date getDateIn_Out() {
        return dateIn_Out;
    }

    /**
     * Date child is signed in or out of day care.
     *
     * @param dateIn_Out the dateIn_Out to set
     */
    public void setDateIn_Out(Date dateIn_Out) {
        this.dateIn_Out = dateIn_Out;
    }

    /**
     * Category in which the child is signed in under.
     *
     * @return the wacthCode
     */
    public WatchCode getWatchCode() {
        return watchCode;
    }

    /**
     * Category in which the child is signed in under.
     *
     * @param watchCode
     */
    public void setWatchCode(WatchCode watchCode) {
        this.watchCode = watchCode;
    }

    /**
     * Hour of day child is signed or out.
     *
     * @return the hour
     */
    public int getHour() {
        return hour;
    }

    /**
     * Hour of day child is signed in or out.
     *
     * @param hour the hour to set
     */
    public void setHour(int hour) {
        this.hour = hour;
    }

    /**
     * Minute of day child is signed in or out. Set to be either 00, 15, 30, 45.
     *
     * @return the minute
     */
    public int getMinute() {
        return minute;
    }

    /**
     * Minute of day child is signed in or out. Set to be either 00, 15, 30, 45.
     *
     * @param minute the minute to set
     */
    public void setMinute(int minute) {
        this.minute = minute;
    }

    /**
     * AM or PM of time.
     *
     * @return the meridiem
     */
    public String getMeridiem() {
        return meridiem;
    }

    /**
     * AM or PM of time.
     *
     * @param meridiem the meridiem to set
     */
    public void setMeridiem(String meridiem) {
        this.meridiem = meridiem;
    }

    /**
     * List of hours used for JSF dropdown.
     *
     * @return the hours
     */
    public String[] getHours() {
        return hours;
    }

    /**
     * List of minutes used for JSF dropdown.
     *
     * @return the minutes
     */
    public String[] getMinutes() {
        return minutes;
    }

    /**
     * AM or PM used for JSF dropdown.
     *
     * @return the meridiem
     */
    public String[] getMeridiems() {
        return meridiems;
    }

    /**
     * List of watchCodes used for JSF dropdown.
     *
     * @return the watchCodes
     */
    public WatchCode[] getWatchCodes() {
        return WatchCode.values();
    }

    /**
     * Minimum date allowed for signing in a child.
     *
     * @return minDate - Earliest date pf birth allowed for day care..
     */
    public Date getMinDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        return c.getTime();
    }

    /**
     * Maximum date allowed for signing in a child.
     *
     * @return maxDate - Today's date.
     */
    public Date getMaxDate() {
        return new Date();
    }

    /**
     * Converts the JSF page results into an SQL timestamp object.
     *
     * @return the converted timestamp
     */
    private Timestamp getTimestamp() {
        Calendar c = Calendar.getInstance();
        c.setTime(getDateIn_Out());
        c.set(Calendar.HOUR, getHour());
        c.set(Calendar.MINUTE, getMinute());
        c.set(Calendar.AM_PM, (getMeridiem().equals("AM")) ? 0 : 1);
        return new Timestamp(c.getTimeInMillis());
    }
}
